package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import com.block.product.service.union.utils.AESUtil;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 乐优花
 */
@Service
public class LeYouHuaProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

            String requestMatchUrl = productInfo.getChannelMatchUrl();
            HttpRequest httpRequest = HttpUtil.createPost(requestMatchUrl);
            httpRequest.header("Content-Type", "application/json");

            String mobileNoMd5 = SecureUtil.md5(userInfoVo.getMobile()).toLowerCase();


            Map<String, Object> bizMap = new HashMap<>();
            bizMap.put("mobileNoMd5", mobileNoMd5);

            String aesKey = RandomUtil.randomString(16);
            log.info("aesKey{}", aesKey);

            String bizContent = AESUtil.strEncodBase64(aesKey, JSONObject.toJSONString(bizMap));
            String secretKey = SecureUtil.rsa(null, productInfo.getPubKey()).encryptBase64(aesKey, KeyType.PublicKey);


            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("secretKey", secretKey);
            paramMap.put("bizContent", bizContent);
            paramMap.put("qd", productInfo.getChannelCode());

            httpRequest.body(JSON.toJSONString(paramMap));

            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【乐优花】撞库完成【resultStr】{}【REQUEST_URL】{}【bizMap】{}【paramMap】{}",
                    resultStr, requestMatchUrl, JSON.toJSONString(bizMap), JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer code = jsonObject.getInteger("code");
            if (!ObjectUtil.equals(200, code)) {
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
            }

            return new ResponseVo(AppRspCodeConstant.SUCCESS);

    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        String requestRegisterUrl = productInfo.getChannelRegisterUrl();

        HttpRequest httpRequest = HttpUtil.createPost(requestRegisterUrl);
        httpRequest.header("Content-Type", "application/json");


        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("mobileNo", userInfoVo.getMobile());
        bizMap.put("ip", userInfoVo.getIp());
        bizMap.put("appType", "0");

        String aesKey = RandomUtil.randomString(16);
        log.info("【aesKey】{}", aesKey);
        String bizContent = AESUtil.strEncodBase64(aesKey, JSONObject.toJSONString(bizMap));
        String secretKey = SecureUtil.rsa(null, productInfo.getPubKey()).encryptBase64(aesKey, KeyType.PublicKey);


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("secretKey", secretKey);
        paramMap.put("bizContent", bizContent);
        paramMap.put("qd", productInfo.getChannelCode());


        httpRequest.body(JSON.toJSONString(paramMap));
        String resultStr = httpRequest.execute().body();
        log.info("【产品联登】【乐优花】【注册】注册完成【result】{}【MATCH_USER_URL】{}【bizMap】{}【paramMap】{}",
                resultStr, requestRegisterUrl, JSON.toJSONString(bizMap), JSON.toJSONString(paramMap));

        JSONObject jsonObject = JSON.parseObject(resultStr);
        Integer code = jsonObject.getInteger("code");
        String message = jsonObject.getString("message");

        if (ObjectUtil.equals(200, code)) {
            String data = jsonObject.getString("data");
            JSONObject resultData = new JSONObject();
            resultData.put("productUrl",data);
            return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
        }
        return new ResponseVo(AppRspCodeConstant.THRID_FAILED, message, "");
    }

    public static void main(String[] args) {
        String biz = "IGEWQ4XfztzS/cLwvjyeCVL2FP5Da4j0e3WxJNFi4R1ABhS9deYDCj16Pc5siuBFPNOoN7uyrTlJv2AKXdjBoQ==";
        String aesKey="mwenjxqrhadrzdq4";
        String decrypt = AESUtil.base64StrDecode(aesKey, biz);
        System.out.println(decrypt);
    }
}
